package com.tboss.template.system.service;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.mail.SimpleMailMessage;
import org.springframework.stereotype.Service;
import org.springframework.ui.Model;
import org.springframework.web.multipart.MultipartFile;

import com.tboss.global.services.mail.VelocityHelperImpl;
import com.tboss.global.services.mail.VelocityUser;
import com.tboss.global.utils.securities.algorithm.SpringSHA;
import com.tboss.global.utils.useful.SecurityUtils;
import com.tboss.template.common.dto.NavigationDto;
import com.tboss.template.common.springSecurity.SecurityService;
import com.tboss.template.common.utils.DateUtils;
import com.tboss.template.system.dao.MappingUserInfoGroupDao;
import com.tboss.template.system.dao.UserInfoDao;
import com.tboss.template.system.dto.UserInfoDto;

@Service
public class UserInfoService {

	@Autowired private SpringSHA springSHA;
	@Autowired SystemService systemService;
	@Autowired private MappingUserInfoGroupService mapUsrInfoGrpService;
	@Autowired private SecurityService securityService;
	@Autowired private MappingUserInfoGroupDao mapUsrInfoGrpDao;
	@Autowired private UserInfoDao usrInfoDao;
	
	private Integer getUsrInfoListCnt(UserInfoDto dto){
		return this.usrInfoDao.getUsrInfoListCnt(dto);
	}
	
	private List<UserInfoDto> getUsrInfoList(UserInfoDto dto){
		return this.usrInfoDao.getUsrInfoList(dto);
	}
	
	public void setModelUsrInfoList(Integer reqPage, Model model, UserInfoDto dto){
		if(reqPage != null) dto.setCurrentPage(reqPage);

		Integer rowsCnt = this.getUsrInfoListCnt(dto);
		NavigationDto pageNavi = new NavigationDto(rowsCnt, dto.getRowPerPage(), dto.getCurrentPage());

		model.addAttribute("sysBase", this.systemService.getSysBase(dto));
		model.addAttribute("pageNavi", pageNavi);
		model.addAttribute("usrInfoList", this.getUsrInfoList(dto));
	}
	
	public void usrRegForm(Model model){
		model.addAttribute("usrGrpList", this.mapUsrInfoGrpDao.getUsrGrpList(null));
		model.addAttribute("usrInfoGrpList", this.mapUsrInfoGrpDao.getUsrInfoGrpList(null));
	}
	
	public void usrRegForm(Model model, String usrId, HttpServletRequest req){
		String isMeParam = req.getParameter("isMe");//From Top-Menu
		Boolean isMe = (isMeParam != null && isMeParam.trim() != "")?Boolean.parseBoolean(isMeParam) : false;
		
		model.addAttribute("isMe", isMe);
		model.addAttribute("usrInfo", this.usrInfoDao.getUsrInfo(usrId));
		model.addAttribute("usrGrpList", this.mapUsrInfoGrpDao.getUsrGrpList(usrId));
		model.addAttribute("usrInfoGrpList", this.mapUsrInfoGrpDao.getUsrInfoGrpList(usrId));
	}
	
	public void usrInfoSendMailForm(Model model, String usrId){
		UserInfoDto dto = this.usrInfoDao.getUsrInfo(usrId);
		model.addAttribute("usrNm", dto.getUsrName());
		model.addAttribute("address", dto.getUsrEmail());
	}
	
	@Autowired VelocityHelperImpl velocityHelper;
	@Autowired SimpleMailMessage templateText;
	public Model usrInfoSendMailFormSend(Model model, VelocityUser dto, List<MultipartFile> files){
		dto.setFrom(this.templateText.getFrom());
		dto.setTemplatePath("configurations/mail/template/sample.vm");
		
		Map<String, Boolean> data = new HashMap<String, Boolean>();
        data.put("result", this.velocityHelper.sendMail(dto, files));
        
        return model.addAttribute("model", data);
	}
	
	public void addUsrInfo(UserInfoDto dto){
		String usrId = SecurityUtils.getCurrentUserName();
		
		dto.setUsrPwd(this.springSHA.encodePassword(dto.getUsrPwd(), null));
		dto.setUsrRegId(usrId);
		dto.setUsrRegDate(DateUtils.getDateFormat("yyyyMMddHHmmss"));
		this.usrInfoDao.addUsrInfo(dto);
		
		dto.setGrpInitChk("1");
		this.mapUsrInfoGrpService.addMapUsrInfoGrp(dto);
	}
	
	public void updateUsrInfo(UserInfoDto dto, HttpServletRequest req){
		String isMeParam = req.getParameter("isMe");//From Top-Menu
		Boolean isMe = (isMeParam != null && isMeParam.trim() != "")?Boolean.parseBoolean(isMeParam) : false;
		
		String usrId = SecurityUtils.getCurrentUserName();
		
		if(dto.getUsrPwd() != null && !dto.getUsrPwd().equals("")){
			dto.setUsrPwd(this.springSHA.encodePassword(dto.getUsrPwd(), null));
		}
		dto.setUsrModId(usrId);
		dto.setUsrModDate(DateUtils.getDateFormat("yyyyMMddHHmmss"));
		this.usrInfoDao.updateUsrInfo(dto);
		
		if(!isMe){//자기 정보 수정이 아니라면... > 관리자에 의해서 수정되는 정보.
			dto.setGrpInitChk("1");
			this.mapUsrInfoGrpService.addMapUsrInfoGrp(dto);
			this.usrInfoDao.updateUsrInfoChgChkField(dto);
		}
	}
	
}
